<?php


namespace app\api\controller\v1;

use app\api\model\Category as CategoryModel;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\Paginate;
use app\common\exception\MissException;

class Category extends Base
{
    /*获取全部分类数据（1级和2级）, 目前只支持最多二级分类
     *  /category/all
     */
    public function getAll($page = 1, $size = 15)
    {
        (new Paginate())->goCheck();
        $pager = CategoryModel::getRawAll($page, $size);
        if ($pager->isEmpty()) {
//            return $this->ok([
//                'data' => [],
//                'current_page' => $pager->currentPage(),
//            ]);
            throw new MissException([
                'msg' => '没找到该分类',
            ]);
        }
        return $this->ok($pager);
    }

    public function getOne($id)
    {
         (new IDMustBePositiveInt())->goCheck();
        $category = CategoryModel::getRawOne($id);
        if (empty($category)) {
            throw new MissException([
                'msg' => 'category没找到',
            ]);
        }
        return $this->ok($category);
    }

    public function getChildren($id)
    {
        $category = CategoryModel::getRawChildren($id);
        if (empty($category)) {
            throw new MissException([
                'msg' => 'category没找到',
            ]);
        }
        return $this->ok($category);
    }


    public function getGridAll()
    {
        return [];
    }
}